package com.ks.xcamera.view;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.mlkit.vision.barcode.common.Barcode;
import com.king.camera.scan.AnalyzeResult;
import com.king.camera.scan.CameraScan;
import com.king.mlkit.vision.barcode.BarcodeCameraScanActivity;
import com.ks.basictools.provider.ContextGlobal;
import com.ks.xcamera.R;
import com.ks.xcamera.constant.Constants;
import com.ks.xcamera.manager.PreferenceManager;
import com.lljjcoder.style.citylist.Toast.ToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BarCodeActivity extends BarcodeCameraScanActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setStatusBar(true, false, R.color.colorTrans);
        super.onCreate(savedInstanceState);

        ImageView logout = findViewById(R.id.logout);
        ImageView data = findViewById(R.id.data);
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestLogout();
            }
        });
        data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(BarCodeActivity.this, DataActivity.class));
            }
        });
    }

    @Override
    public void initCameraScan(@NonNull CameraScan<List<Barcode>> cameraScan) {
        super.initCameraScan(cameraScan);
        getCameraScan().setPlayBeep(true)
                .setAnalyzeImage(true)
                .setVibrate(true);
    }

    @Override
    public void onScanResultCallback(@NonNull AnalyzeResult<List<Barcode>> result) {
        if (result.getResult().size() > 0) {
            String displayValue = result.getResult().get(0).getDisplayValue();
            // 停止分析
            getCameraScan().setAnalyzeImage(false);
            // 返回结果
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra(CameraScan.SCAN_RESULT, displayValue);
            startActivity(intent);
            Log.d("zhxk", "onScanResultCallback: displayValue = " + displayValue);
        }
    }

    @Override
    public void onScanResultFailure() {
        super.onScanResultFailure();
        getCameraScan().setAnalyzeImage(true);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_qrcode_scan;
    }

    @Override
    protected void onResume() {
        super.onResume();
        getCameraScan().setAnalyzeImage(true);
    }

    private void requestLogout() {
        Map<String, Object> param = new HashMap<>();
        param.put("token", PreferenceManager.getInstance().getToken(ContextGlobal.get().getContext()));
        XHttp.post(Constants.URL_LOGOUT)
                .params(param)
                .execute(new SimpleCallBack<Object>() {

                    @Override
                    public void onSuccess(Object bean) {
                        PreferenceManager.getInstance().setToken(BarCodeActivity.this, "");
                        startActivity(new Intent(BarCodeActivity.this, LoginActivity.class));
                        finish();
                    }

                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showLongToast(ContextGlobal.get().getContext(), e.getMessage());
                    }
                });
    }

    /**
     * 设置状态栏是否全屏
     *
     * @param fullScreen      true：全屏；false：非全屏
     * @param darkUI          true：图标文字为深色；false：图标文字为亮色
     * @param backgroundColor 状态栏背景色 R.color.xxx
     */
    public void setStatusBar(boolean fullScreen, boolean darkUI, @ColorRes int backgroundColor) {
        /*设置状态栏*/
        View decorView = getWindow().getDecorView();
        int vis;
        //View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR：设置状态栏中的文字颜色和图标颜色为深色，不设置默认为白色，需要android系统6.0以上。
        if (fullScreen) {
            if (darkUI) {
                vis = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            } else {
                vis = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_VISIBLE;
            }
        } else {
            if (darkUI) {
                vis = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            } else {
                vis = View.SYSTEM_UI_FLAG_VISIBLE;
            }
        }
        decorView.setSystemUiVisibility(vis);
        if (backgroundColor != 0) {
            //设置状态栏规定的颜色
            getWindow().setStatusBarColor(getResources().getColor(backgroundColor, null));
        } else {
            //设置状态栏的默认背景颜色为白色
            getWindow().setStatusBarColor(getResources().getColor(R.color.white, null));
        }
    }
}